/**
 * @(#)MobileClientServiceImpl.java 2017年6月7日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.bto.AppInfo;
import org.jsbd.boss.bto.HttpResult;
import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMobileClientDAO;
import org.jsbd.boss.domian.channel.AndriodVerClient;
import org.jsbd.boss.domian.channel.MobileClient;
import org.jsbd.boss.service.IMobileClientService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;

import edu.hziee.common.queue.DelayExecuteBuffer;
import edu.hziee.common.tcp.cache.CachedHolder;

/**
 * TODO 请加入本类的说明
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class MobileClientServiceImpl implements IMobileClientService {
	public static final Logger LOGGER = LoggerFactory.getLogger(MobileClientServiceImpl.class);

	@Resource(name = "mobileClientDao")
	private IMobileClientDAO mobileClientDao;

	@Resource
	private DelayExecuteBuffer<MobileClient> mobileClientInsertBuffer;

	@Resource
	private RedisClient redisClient;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.jsbd.boss.service.IMobileClientService#selectMobileClient(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public MobileClient selectMobileClient(String hstype, String hsman) {
		String key = CacheKey.MOBILE_CLIENT + hsman + hstype;
		MobileClient client = (MobileClient) redisClient.get(key);
		if (client != null) {
			return (MobileClient) client;
		} else {
			MobileClient mobileClient = mobileClientDao.selectMobileClient(hstype, hsman);
			if (mobileClient != null) {
				redisClient.put(key, mobileClient, 86400);
			}
			return mobileClient;
		}
	}

	@Override
	public MobileClient randomSelectMobileClient(String hsman) {
		return mobileClientDao.randomSelectMobileClient(hsman);
	}

	@Override
	public List<AndriodVerClient> selectAndriodVerClientList(String ver) {
		return mobileClientDao.selectAndriodVerClientList(ver);
	}

	@Override
	public void addMobileClient(HttpResult httpResult) {
		String hsman = httpResult.getHsman();
		String hstype = httpResult.getHstype();
		if (StringUtils.isNotBlank(hsman) && StringUtils.isNotBlank(hstype)) {
			String key = CacheKey.HSMAN_HSTYPE_RECORD;
			String value = hsman.toLowerCase() + hstype.toLowerCase();
			Boolean isExists = redisClient.isMembers(key, value);
			if (isExists == null || !isExists) {
				MobileClient mobileClient = initMobileClient(httpResult);
				mobileClientInsertBuffer.add(mobileClient);
				redisClient.sadd(key, value);
			}
		}

	}

	private MobileClient initMobileClient(HttpResult httpResult) {
		MobileClient clientInfo = new MobileClient();
		clientInfo.setDensityDpi(httpResult.getDensityDpi());
		clientInfo.setBuildId(httpResult.getBuildId());
		if (StringUtils.isNotBlank(httpResult.getLac())) {
			clientInfo.setLac(Integer.parseInt(httpResult.getLac()));
		}
		if (StringUtils.isNotBlank(httpResult.getCellId())) {
			clientInfo.setCellId(Integer.parseInt(httpResult.getCellId()));
		}
		clientInfo.setLongitude(httpResult.getLongitude());
		clientInfo.setLatitude(httpResult.getLatitude());
		clientInfo.setHsman(httpResult.getHsman());
		clientInfo.setHstype(httpResult.getHstype());
		clientInfo.setOsVer(httpResult.getOsVer());
		short width = 0, height = 0;
		String screenWidth = httpResult.getScreenWidth();
		String screenHeight = httpResult.getScreenHeight();
		if (StringUtils.isNotBlank((screenWidth))) {
			width = Short.parseShort(screenWidth);
		}
		if (StringUtils.isNotBlank((screenHeight))) {
			height = Short.parseShort(screenHeight);
		}
		double xdpi = 0, ydpi = 0;
		Double xdpi1 = httpResult.getXdpi();
		Double ydpi1 = httpResult.getYdpi();
		if (xdpi1 != null) {
			xdpi = xdpi1;
		}
		if (ydpi1 != null) {
			ydpi = ydpi1;
		}
		BigDecimal y = new BigDecimal(ydpi);
		ydpi = y.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

		BigDecimal x = new BigDecimal(xdpi);
		xdpi = x.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		if (width > height) {
			clientInfo.setScreenWidth(height);
			clientInfo.setScreenHeight(width);
			clientInfo.setXdpi(ydpi);
			clientInfo.setYdpi(xdpi);
		} else {
			clientInfo.setScreenWidth(width);
			clientInfo.setScreenHeight(height);
			clientInfo.setXdpi(xdpi);
			clientInfo.setYdpi(ydpi);
		}
		clientInfo.setCpu(httpResult.getCpu());
		clientInfo.setSdId(httpResult.getSd_id());
		if (StringUtils.isNotBlank(httpResult.getBuild_version())) {
			clientInfo.setBuildVersion(httpResult.getBuild_version());
		}
		if (StringUtils.isNotBlank(httpResult.getOsVer())) {
			String[] osVers = httpResult.getOsVer().split("_");
			if (osVers.length == 1) {
				clientInfo.setRelease(osVers[0]);
			} else {
				clientInfo.setRelease(osVers[1]);
			}
		}
		clientInfo.setProductName(httpResult.getProductName());
		clientInfo.setProductDevice(httpResult.getProductDevice());
		clientInfo.setDensity(httpResult.getDensity());
		if (StringUtils.isNotBlank(httpResult.getAppInfo())) {
			Gson gson = new Gson();
			AppInfo appInfo = gson.fromJson(httpResult.getAppInfo(), AppInfo.class);
			clientInfo.setAppName(appInfo.getApp_name());
			clientInfo.setSystemAppName(appInfo.getSystem_app_name());
		}
		clientInfo.setUserAgent(httpResult.getUserAgent());
		clientInfo.setBoard(httpResult.getBoard());
		clientInfo.setCpuSerial(httpResult.getCpuSerial());
		clientInfo.setKernelVer(httpResult.getKernelVersion());
		clientInfo.setSdkUuid(httpResult.getUuid());
		clientInfo.setSensorInfo(httpResult.getSensorInfo());
		return clientInfo;
	}
}
